package com.atguigu.controller;

import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RolePermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-01 12:21
 * @description：
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    @DubboReference
    private RoleService roleService;
    @DubboReference
    private PermissionService permissionService;
    @DubboReference
    private RolePermissionService rolePermissionService;

    @RequestMapping
    @PreAuthorize("hasAuthority('role.show')")
    public String index(Map map, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        map.put("filters", filters);
        PageInfo<Role> list = roleService.findPage(filters);
        map.put("page", list);
        return "role/index";
    }

    @RequestMapping("/save")
    @PreAuthorize("hasAuthority('role.create')")
    public String save(Role role) {
        roleService.insert(role);
        return "common/success";
    }

    @RequestMapping("/edit/{id}")
    @PreAuthorize("hasAuthority('role.edit')")
    public String edit(@PathVariable Long id, Map map) {
        Role role = roleService.findById(id);
        map.put("role", role);
        return "role/edit";
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('role.edit')")
    public String edit(Role role) {
        roleService.update(role);
        return "common/success";
    }

    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('role.delete')")
    public String delete(@PathVariable Long id) {
        roleService.delete(id);
        return "common/success";
    }

    @RequestMapping("/assignShow/{roleId}")
    @PreAuthorize("hasAuthority('role.delete')")
    public String assignShow(@PathVariable Long roleId, Map map) {
        //1.将roleId返回
        map.put("roleId", roleId);
        //2.返回List<Map<String,Object>>的数据类型:{id:1,pid:2,name:啊啊啊,checked:true}
        List<Map<String, Object>> nodes = permissionService.findNodes(roleId);
        map.put("zNodes", nodes);
        return "role/assignShow";
    }

    @RequestMapping("/assignPermission")
    @PreAuthorize("hasAuthority('role.assign')")
    public String assignPermission(Long roleId, String[] permissionIds) {
        //调用业务逻辑
        rolePermissionService.insertDate(roleId, permissionIds);
        return "common/success";
    }
}
